package com.isunland.managesystem.utils;

import com.google.gson.Gson;
import com.isunland.managesystem.entity.LocalConfigOriginal;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyanan on 2016/5/5.
 * 动态配置生成工具
 */
public class LocalConifgGenerateUtil {
  public static final String KEY = "key";
  public static final String TITLE = "title";
  public static final String VISIBILITY = "visibility";

  private static ArrayList<LocalConfigOriginal> sList = new LocalConfigOriginal().generate();

  public static void getJson() {
    for (LocalConfigOriginal original : sList) {
      String json = new Gson().toJson(original);
      FileUtil.stringToFile(json, new File(FileUtil.getCacheDir(),
          original.getMessage() + "_" + original.getResult() + ".json"));
    }
  }

  public static void getString() {

    StringBuilder builder = new StringBuilder();
    //头.
    builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<resources>\n");
    ArrayList<String> keyList = new ArrayList<String>();
    ArrayList<String> moduleList = new ArrayList<String>();
    for (LocalConfigOriginal original : sList) {
      List<LocalConfigOriginal.LocalConfigModel> list = original.getRows();

      String module = original.getMessage();
      if (!moduleList.contains(module)) {
        moduleList.add(module);
        //模块
        builder.append("<!--模块 : " + module + "-->\n");
        builder.append("<string name=\"" + "module_" + module + "\">" + module + "</string>\n");
      }
      for (LocalConfigOriginal.LocalConfigModel model : list) {
        String field = model.getField();
        String title = model.getTitle();
        boolean isHidden = model.isHidden();

        if (!keyList.contains(module + "_" + field)) {
          keyList.add(module + "_" + field);
          builder.append("<!--" + title + "-->\n");
          builder.append(
              "<string name=\"" + module + "_" + field + "_" + KEY + "\">" + field + "</string>\n");
          builder.append("<string name=\""
              + module
              + "_"
              + field
              + "_"
              + TITLE
              + "\">"
              + module
              + "_"
              + field
              + "_"
              + TITLE
              + "</string>\n");
          builder.append("<string name=\""
              + module
              + "_"
              + field
              + "_"
              + VISIBILITY
              + "\">"
              + module
              + "_"
              + field
              + "_"
              + VISIBILITY
              + "</string>\n");
        }
      }
    }

    //尾
    builder.append("</resources>");

    //       LogUtil.i("生成配置string为:"+ builder.toString());
    FileUtil.stringToFile(builder.toString(),
        new File(FileUtil.getCacheDir(), "string_config.xml"));
  }
}
